﻿namespace UnityEngine.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using Ionic.Zip;
    using UnityEngine.Core;
    using System.Xml.Serialization;

    public class IOManager
    {
        public static readonly string AssetsFolderName = "Assets";
        private static IOManager instance = null;

        public static IOManager Instance
        {
            get
            {
                if (IOManager.instance == null)
                {
                    IOManager.instance = new IOManager();
                }
                return IOManager.instance;
            }
        }

        public string AssetsFolder
        {
            get
            {
                return Directory.GetCurrentDirectory() + "\\" + AssetsFolderName;
            }
        }

        public string GetAssertPath(string path)
        {
            if (path.Contains(AssetsFolderName))
                path = path.Remove(0, path.IndexOf(AssetsFolderName) + 7);
            return this.AssetsFolder + "\\" + path;
        }

        public string GetPathUnderAssets(string path, string suffix)
        {
            if (path.Contains(suffix))
                return this.AssetsFolder + "\\" + path;
            else
                return this.AssetsFolder + "\\" + path + "." + suffix;
        }

        public string GetRelativePathUnderAssets(string path)
        {
            if (path.IndexOf(this.AssetsFolder) == 0)
                path = path.Remove(0, this.AssetsFolder.Length + 1);
            return path;
        }

        public bool IsValidSceneFile(string path, bool isFullPath = false)
        {
            bool ret = true;
            if (isFullPath == false)
                path = GetPathUnderAssets(path, GameObject.GameSceneFileSuffix);
            ret = File.Exists(path) && path.EndsWith(GameScene.GameSceneFileSuffix);
            return ret;
        }

        public GameScene LoadGameScene(string path, bool isFullPath = false)
        {
            GameScene gameScene = new GameScene();
            if (isFullPath == false)
                gameScene.Path = path;
            else
                gameScene.Path = GetRelativePathUnderAssets(path);
            if (isFullPath == false)
                path = GetPathUnderAssets(path, GameObject.GameSceneFileSuffix);
            using (FileStream filestream = File.OpenRead(path))
            {
                MemoryStream stream = new MemoryStream();
                stream.SetLength(filestream.Length);
                filestream.Read(stream.GetBuffer(), 0, (int)filestream.Length);
                gameScene.LoadMemoryStream(stream);
                stream.Close();
                filestream.Close();
            }

            gameScene.TryToAddConfiguration();
            gameScene.TryToAddDebug();
            return gameScene;//----------------------------------------------------------------
            // Copyright (c) 2011-2013 
            // All rights reserved.
            // Author: Ben DaoWei 
            // Contact: bendaowei@gmail.com
            // Web Link: http://xnaeditor.codeplex.com
            // ----------------------------------------------------------------


        }

        public void SaveGameScene(GameScene gameScene, string path, bool isFullPath = false)
        {
            if (isFullPath == false)
                path = GetPathUnderAssets(path, GameObject.GameSceneFileSuffix);
            ZipFile zipFile = gameScene.ToZipFile();
            zipFile.Save(path);
        }

        public GameObject LoadGameObject(string path, bool isFullPath = false)
        {
            GameObject gameObject = new GameObject();
            gameObject.Name = Path.GetFileNameWithoutExtension(path);
            if (isFullPath == false)
                gameObject.PrefabPath = path;
            else
                gameObject.PrefabPath = GetRelativePathUnderAssets(path);
            if (isFullPath == false)
                path = GetPathUnderAssets(path, GameObject.GameObjectFileSuffix);
            using (FileStream filestream = File.OpenRead(path))
            {
                MemoryStream stream = new MemoryStream();
                stream.SetLength(filestream.Length);
                filestream.Read(stream.GetBuffer(), 0, (int)filestream.Length);
                gameObject.LoadMemoryStream(stream);
                stream.Close();
                filestream.Close();
            }

            return gameObject;
        }

        public void SaveGameObject(GameObject gameObject, string path, bool isFullPath = false)
        {
            if (isFullPath == false)
                path = GetPathUnderAssets(path, GameObject.GameObjectFileSuffix);
            gameObject.PrefabPath = this.GetRelativePathUnderAssets(path);
            ZipFile zipFile = gameObject.ToZipFile();
            zipFile.Save(path);
        }
    }
}
